<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录</title>
    <link rel="stylesheet" href="css/bootstrap.min.css">
    <link rel="stylesheet" href="css/login.css">
    <script type="text/javascript" src="js/jquery.min.js"></script>
</head>

<body>
    <div class="container-login">
        <div class="container-pic">
            <img src="pic/computer.png" width="350px">
        </div>
        <div class="login-dialog">
            <h3>图书管理系统</h3>
            <div class="row">
                <span><label for="userName">用户名</label></span>
                <input type="text" name="userName" id="userName" class="form-control" placeholder="请输入用户名">
            </div>
            <div class="row">
                <span><label for="password">密码</label></span>
                <input type="password" name="password" id="password" class="form-control" placeholder="请输入密码">
            </div>
            <div class="row">
                <button type="button" class="btn btn-info btn-lg" onclick="login()">登录</button>
                没有账号?<a href="./register.html">去注册</a>
            </div>
        </div>
    </div>
    <script src="js/jquery.min.js"></script>
    <script>
        function login() {
             // 1、获取输入框的值，构造JSON数据
             let userName = $('#userName').val();
            let password = $('#password').val();
            if (userName == '' || password == '') {
                alert('请输入完整的登录信息');
                return;
            }
            let user = JSON.stringify({
                // 这里的key要和后端接收参数的属性对应
                "username": userName,
                "password": password
            });

            // 2、构造Ajax请求，并请求后端
            $.ajax({
                type: "post",
                url: '/user/login',
                contentType: 'application/json',
                data: user,
                // 这里的成功是指http请求成功，并不是业务
                success(result) {
                    if (result.resultCodeEnum == "SUCCESS") {
                        alert('登录成功');
                        location.href = "./book_list.html"; // 跳转到图书列表页面
                    } else if (result.resultCodeEnum == "ERROR") {
                        alert(result.message);
                    } else {
                        alert('服务异常');
                    }
                },
                error() {
                    alert('HTTP请求失败');
                }
            });
        }
    </script>
</body>

</html>